Skip to content

fix(pm): preserve message WebView state across recreation (#691)#811

Open
jim-daf wants to merge 1 commit intoAwful:developfrom
jim-daf:fix/issue-691-pm-webview-state
Open

fix(pm): preserve message WebView state across recreation (#691)#811
jim-daf wants to merge 1 commit intoAwful:developfrom
jim-daf:fix/issue-691-pm-webview-state

Conversation

@jim-daf
Copy link
Copy Markdown

@jim-daf jim-daf commented Apr 25, 2026

Closes #691.

Reopening the PM view after popping back from the settings activity (and on configuration changes in general) shows a blank message body. The fragment uses setRetainInstance(true) for the wrapper but the embedded AwfulWebView is rebuilt and its state is not preserved.

This change persists the WebView in onSaveInstanceState, restores it in onCreateView, and skips the syncPM() refetch when there is a snapshot to restore. The blank screen goes away and the existing first-load path is unchanged.

@Override
public void onSaveInstanceState(@androidx.annotation.NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    if (messageWebView != null) {
        messageWebView.saveState(outState);
    }
}
if (aSavedState != null) {
    messageWebView.restoreState(aSavedState);
}

if (pmId <= 0) {
    messageWebView.setVisibility(GONE);
} else if (aSavedState == null) {
    syncPM();
}

@jim-daf jim-daf marked this pull request as ready for review April 25, 2026 19:28
Copilot AI review requested due to automatic review settings April 25, 2026 19:28
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the PM detail view showing a blank message body after returning from Settings or other fragment recreation events by persisting/restoring the embedded AwfulWebView state and avoiding an unnecessary refetch when a snapshot is available.

Changes:

  • Save the message AwfulWebView state in onSaveInstanceState.
  • Restore the saved AwfulWebView state in onCreateView.
  • Skip syncPM() when restoring from saved state.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +114 to +118
// Issue #691: persist the message WebView so re-entering the
// fragment after the settings activity does not redraw blank.
if (messageWebView != null) {
messageWebView.saveState(outState);
}
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebView#saveState writes keys directly into the provided Bundle; to avoid accidental key collisions with fragment/view state and to make it possible to detect whether anything was actually saved, consider saving into a dedicated nested Bundle (e.g., outState.putBundle(...)) and/or tracking saveState’s return value.

Copilot uses AI. Check for mistakes.
Comment on lines 145 to +151
messageWebView.setContent(AwfulHtmlPage.getContainerHtml(mPrefs, null, false));

// Issue #691: restore the saved WebView snapshot when the
// fragment is recreated so the message body does not come back
// blank after popping back from the settings activity.
if (aSavedState != null) {
messageWebView.restoreState(aSavedState);
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

restoreState is being called after setContent(...) has already loaded the container HTML. Per WebView lifecycle expectations, restore is most reliable before any load; consider restoring first (or skipping setContent entirely when restoring) to avoid overwriting/restoring inconsistently or causing an extra blank load/flicker.

Suggested change
messageWebView.setContent(AwfulHtmlPage.getContainerHtml(mPrefs, null, false));
// Issue #691: restore the saved WebView snapshot when the
// fragment is recreated so the message body does not come back
// blank after popping back from the settings activity.
if (aSavedState != null) {
messageWebView.restoreState(aSavedState);
// Issue #691: restore the saved WebView snapshot when the
// fragment is recreated so the message body does not come back
// blank after popping back from the settings activity.
if (aSavedState != null) {
messageWebView.restoreState(aSavedState);
} else {
messageWebView.setContent(AwfulHtmlPage.getContainerHtml(mPrefs, null, false));

Copilot uses AI. Check for mistakes.
Comment on lines 154 to 157
if(pmId <=0){
messageWebView.setVisibility(GONE);
}else{
}else if (aSavedState == null) {
syncPM();
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using aSavedState == null as the only gate for syncPM() can skip the network fetch even when no WebView state was actually restored (e.g., saveState returned null / state missing in the Bundle). Consider gating on “restore succeeded” (e.g., restoreState return value != null, or presence of a dedicated saved Bundle) rather than just Bundle non-nullness.

Copilot uses AI. Check for mistakes.
Comment on lines +112 to +113
public void onSaveInstanceState(@androidx.annotation.NonNull Bundle outState) {
super.onSaveInstanceState(outState);
Copy link

Copilot AI Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style consistency: elsewhere in the codebase @NonNull is imported (import androidx.annotation.NonNull) rather than using the fully-qualified @androidx.annotation.NonNull in method signatures. Importing NonNull here would match the existing convention.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PM view doesn't maintain webview state

2 participants